<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2019/7/14
 * Time: 15:05
 */

namespace app\admin\controller;

use think\Db;

class Devcontroller extends Common
{

    /**
     * @return mixed
     * 控制器列表
     */
    public function DevcontrollerList()
    {
        if (request()->isPost()) {
            $key = input('post.key') ? input('key') : '';
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $list = Db('nyd_control_dev')
                ->alias('a')
                ->join('nyd_gateway c', 'a.gateway_id=c.id')
                ->join('nyd_house b', 'c.house_id = b.id')
                ->field('a.id,b.house_name,c.gateway_id,a.is_bad,a.is_open,a.control_id, c.gateway_name,c.gateway_id,a.control_shortname,a.control_longname')
                ->where('a.is_del', 1)
                ->where('a.control_id', 'like', "%" . $key . "%")
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            foreach ($list['data'] as $k => $v) {
                if ($list['data'][$k]['is_bad'] == 1) {
                    $list['data'][$k]['is_bad'] = '正常';
                } else if ($list['data'][$k]['is_bad'] == 2) {
                    $list['data'][$k]['is_bad'] = '故障';
                }
            }
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return $this->fetch('dev_controller/dev_controller_list');
    }

    /**
     * kongzhiqizhuangtaikaiguan
     * -----wangpengyang  2019-7-14
     */
    public function DevControllerState()
    {
        $id = input('post.id');
        $is_open = input('post.is_open');
        if (empty($id)) {
            return $this->error('设备不存在!');
        }
        db('nyd_control_dev')->where('id', $id)->update(['is_open' => $is_open]);
        return $this->success('设备状态修改成功');
    }


    /**
     *shanchu
     *----wangpengyang
     */
    public function DevControllerDel()
    {
        $id = input('post.id');
        if (empty($id)) {
            return $this->error('设备不存在!');
        }
        $is_del = 2;
        db('nyd_control_dev')->where('id', $id)->update(['is_del' => $is_del]);
        return $this->success('设备删除成功');
    }

    /**
     * @return mixed
     * 添加控制器
     * ---2019-7-14
     */
    public function devControllerAdd()
    {

        if (request()->isAjax()) {
            if(empty(input('post.control_id'))){
                $house_id = input('post.')?input('post.'):'';
                //网关
                $gateway = Db::name('nyd_gateway')->where('house_id',$house_id['house_id'])->select();
                return $gateway;
            }
            else{
                if (request()->isPost()) {
                    $data = input('post.');

                       $res = Db::name('nyd_control_dev')->where('control_id', $data['control_id'])->where("gateway_id",$data['gateway_id'])->where('is_del', 1)->find();
                    if ($res) {
                        $this->error('已存在该控制器');
                    }
                    //插入设备
                    $re = Db::name('nyd_control_dev')->insert($data);
                    if ($re) {
                        $this->success('添加成功');
                    } else {
                        $this->error('添加失败');
                    }
                }
            }
        }
        //选择大棚
        $house = Db::name('nyd_house')->field('house_name,id')->select();
        $this->assign('house', $house);

        return $this->fetch('dev_controller/dev_controller_add');

    }


    /**
     * @return mixed|void
     * bianji
     */

    public function devControllerEdit()
    {

        if (request()->isAjax()) {
            if(empty(input('post.control_id'))){
                $house_id = input('post.')?input('post.'):'';
                //网关
                $gateway = Db::name('nyd_gateway')->where('house_id',$house_id['house_id'])->select();
                foreach ($gateway as $key => $value){
                    $gateway[$key]['gateway'] = $gateway[$key]['gateway_id'] . '--' . $gateway[$key]['gateway_name'];
                }
                return $gateway;
            }
            else{
                if (request()->isPost()) {
                    $data = input('post.');
                    $re = Db::name('nyd_control_dev')->where('id', $data['id'])->update($data);
                    if ($re) {
                        $this->success('修改成功');
                    } else {
                        $this->error('修改失败');
                    }

                }
            }
        }
        $id = input('get.id');
        if (empty($id)) {
            return $this->error('设备不存在!');
        }
        $house = Db::name('nyd_house')->select();
        $this->assign('house', $house);

        $this->assign('id', $id);
        $result = Db::name('nyd_control_dev')
            ->alias('a')
            ->join('nyd_gateway c', 'a.gateway_id=c.id')
            ->join('nyd_house b', 'c.house_id = b.id')
            ->field('a.id,a.is_bad,b.house_name,a.control_id,a.control_relation,a.open_imgUrl,a.close_imgUrl, c.gateway_name,a.gateway_id,c.house_id,a.control_shortname,a.control_longname')
            ->where('a.id', $id)->find();
        $result['gateway'] = $result['gateway_id'] . '--' . $result['gateway_name'];
        $result['control_relation'] = explode(',',$result['control_relation']);
        $this->assign('info', $result);
        $this->assign('control_relation', json_encode($result['control_relation']));

        // 获取control_ids
        $gateway_id = Db::name('nyd_control_dev')->where('id',$id)->value('gateway_id');
        $control_ids = Db::name('nyd_control_dev')->where('id','neq',$id)->where('gateway_id',$gateway_id)->column('id,control_id,control_shortname');
        $this->assign('control_ids',$control_ids);
        
        return $this->fetch('dev_controller/dev_controller_edit');
    }

}